﻿using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using UFIDA.U9.BS.SiteKeyProviders;
using UFIDA.U9.WSS.ProxyService.Host;
using UFSoft.UBF.Engine;
using UFSoft.UBF.Service;

namespace UFIDA.U9.WSS.ProxyService.Services
{
    public class IISHostFactory : ServiceHostFactory
    {
        private static readonly object LockObject = new object();

        static IISHostFactory()
        {
            Init();
        }

        public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
        {
            return CreateServiceHost(Type.GetType(constructorString), baseAddresses);
        }

        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            return new IISHost(serviceType, baseAddresses);
        }

        private static void Init()
        {
            lock (LockObject)
            {
                //重新注册服务校验服务
                IServiceCheckProvider provider = ServiceProvider.Instance.GetService<IServiceCheckProvider>();
                if (!(provider is PortalSiteKeyProvider)) return;
                ServiceProvider.Instance.RemoveService(provider);
                ServiceProvider.Instance.AddService(new WSSPortalSiteKeyProvider(provider as PortalSiteKeyProvider));
            }
        }
    }
}